<?php

namespace App\Models;

use App\Lib\Uploads\UploadHandler;
use App\Models\Posts\WxPost;
use App\Models\Posts\WxPostsVideo;
use App\Models\User\WxUser;
use App\Wen\Utils\FileUtils;
use App\Wen\Utils\UserUtils;
use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;

class WxAttachment extends Model
{
	use HasDateTimeFormatter;
    protected $table = 'wx_attachment';

    protected static function boot()
    {
        parent::boot();

        static::updating(function ($model) {
            // 在模型更新之前触发的操作
            $model->original = $model->getOriginal();
        });
        static::updated(function ($model) {
            $original = $model->original;
            $changes = $model->getChanges();

            $is_state_or_review_change = false;
            // 对比更新前后的字段
            foreach ($changes as $key => $value) {
                if($key == 'state' || $key == 'cos_review'){
                    $is_state_or_review_change = true;
                    $originalValue = $original[$key] ?? 0;
                }
            }

            if($is_state_or_review_change){
                if($model->type == 'mp4'){
                    if($model->state == 3 || $model->state == 4){
                        // 疑似
                        $postvideo = WxPostsVideo::where('video_url', $model->domain . '/' . $model->path)->first();
                        if($postvideo){
                            WxPostsVideo::where('video_url', $model->domain . '/' . $model->path)->update(['posts_video_state'=>4]);
                            WxPost::where('id',$postvideo->posts_id)->update(['is_examine'=>0]);
                        }
                    }else if($model->state == 2){
                        // 确定违规
                        $postvideo = WxPostsVideo::where('video_url', $model->domain . '/' . $model->path)->first();
                        if($postvideo){
                            WxPostsVideo::where('id', $postvideo->id)->update(['posts_video_state'=>5]);

                            WxPost::where('id',$postvideo->posts_id)->update(['is_examine'=>2]);
                            if($postvideo->video_thumb_url){
                                $cover = FileUtils::get_url_attach($postvideo->video_thumb_url);
                                UploadHandler::del($cover);
                            }
                            $the_user = WxUser::find($postvideo->user_id);
                            UserUtils::lock_user_incre($the_user, 3, 3600 * 24, '发布违规视频');
                            UserUtils::add_user_notice(2008, $postvideo->user_id, '视频审核结果：确定违规',
                                '「<a href="/pages/sticky/sticky?id='.$postvideo->posts_id.'">笔记（id：'.$postvideo->posts_id.'）</a>」中发布的视频违规，请勿发布黄色，广告，政治等敏感视频，笔记已驳回', 100);
                        }
                    }else{
                        _logger_(__file__, __line__, '11111111111111');
                        WxPostsVideo::where('video_url', $model->domain . '/' . $model->path)->update(['posts_video_state'=>0]);
                    }
                }
            }

        });

        static::deleted(function ($model) {
            UploadHandler::del($model, false);
        });
    }

    public function user()
    {
        return $this->hasOne(WxUser::class, 'id', 'user_id');
    }
}
